package com.graduation.smartparkingplatform.Controller;

import com.github.pagehelper.PageInfo;
import com.graduation.smartparkingplatform.Pojo.Company;
import com.graduation.smartparkingplatform.Response.Response;
import com.graduation.smartparkingplatform.Response.ResponseUtils;
import com.graduation.smartparkingplatform.Service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/company")
public class CompanyController {

  @Autowired CompanyService companyService;

  // 获取公司列表
  @GetMapping("/companyList")
  public Response getCompanyList(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count) {
    try {
      PageInfo<Company> companyList = companyService.getCompanyList(page, count);
      return ResponseUtils.buildSuccess(companyList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 模糊搜索
  @GetMapping("/searchCompany")
  public Response searchCompany(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count,
      @RequestParam(value = "companyName", defaultValue = "null") String companyName,
      @RequestParam(value = "companyDirector", defaultValue = "null") String companyDirector) {
    try {
      PageInfo<Company> searchList =
          companyService.searchCompany(page, count, companyName, companyDirector);
      return ResponseUtils.buildSuccess(searchList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 增加合作公司
  @PostMapping("/addCompany")
  public Response addCompany(@RequestBody Company company) {
    Boolean addCompany = companyService.addCompany(company);
    if (addCompany) {
      return ResponseUtils.buildSuccess("增加合作公司成功");
    }
    return ResponseUtils.buildError("增加合作公司失败");
  }

  // 修改合作公司
  @PostMapping("/changeInfo")
  public Response changeInfo(@RequestBody Company company) {
    Boolean changeInfo = companyService.changeInfo(company);
    if (changeInfo) {
      return ResponseUtils.buildSuccess("修改合作公司信息成功");
    }
    return ResponseUtils.buildError("修改合作公司信息失败");
  }

  // 修改合作单位状态
  @PostMapping("/changeStatus")
  public Response changeCompanyStatus(
      @RequestParam("companyId") Integer companyId, @RequestParam("status") Boolean status) {
    Boolean changeStatus = companyService.changeCompanyStatus(companyId, status);
    if (changeStatus) {
      return ResponseUtils.buildSuccess("修改合作单位状态成功");
    }
    return ResponseUtils.buildError("修改合作单位状态失败");
  }

  // 删除合作单位
  @PostMapping("/deleteCompany")
  public Response deleteCompany(@RequestParam("companyId") Integer companyId) {
    Boolean delete = companyService.deleteCompany(companyId);
    if (delete) {
      return ResponseUtils.buildSuccess("删除合作单位成功");
    }
    return ResponseUtils.buildError("删除合作单位失败");
  }
}
